/*
navigation_mag.h
offer navigation_mag.c  head file and api to outside
designed by lunanting

-------------------------
2020-03-26 17:41:31
create file
-------------------------
*/
#ifndef __MAG_H
#define __MAG_H

#include "stdint.h"
#include "aos/hal/uart.h"
#include "modbus_485.h"

#define MAG_SLAVE_ID    0x06    //从机地址
#define MAG_DIR_RES     2000  //dir set reginster
#define MAG_STATE_RES   1001  //mag line number
#define MAG_DIS_RES     1002  //mag line distance to sensor middle


/********************type define******************/
//agreement type
typedef enum{
    mag_hs=0,   //hs私有协议
    mag_modbus //midbus协议
}MAG_agreement;


//road choose
typedef enum{
    mag_middle=0, //直行
    mag_left,   //左转
    mag_right   //右转
}MAG_dir;

//mag location data
typedef struct {
    uint8_t sensor;   //sensor state 0=正常，or error code
    uint8_t mag;   //mag value
    short left;  //left mag location
    short middle;    //middle mag location
    short right; //right mag location
}MAG_location;


//mag state data
typedef struct{
    uart_dev_t uart;    //use uart
    MAG_location location;  //mag location data
    MAG_agreement agreement;    //agreement type
    uint8_t modbus_port;    //modbus port
    MB_data modbus; //modbus data
    uint8_t read_data[10];  //save read data buffer
    uint8_t read_len;   //save read data length
    int init;   //init state,0=uninit,1=init as modbus,2=init as hs
}MAG_state;




/***************************function extern**********************/
extern int xp_mag_init(uint8_t port,MAG_agreement agreement,int speed,uint8_t slave_id); //mag init
extern int xp_mag_location(MAG_location *data);    //get mag location
extern int xp_mag_recive_analysis_hs(uint8_t *data);   //analysis mag recive data

extern int xp_mag_get_location_hs(void);    //get mag location by HS agreement
extern int xp_mag_recive_analysis_modbus(void); //analysis mag recive data
extern int xp_mag_get_location_modbus(void);    //get mag location by modbus

extern int xp_mag_set_dir(MAG_dir dir); //set mag dir

extern uint16_t xp_mag_crc(uint8_t *data,int len);  //mag  crc

extern void xp_mag_test(void);  //mag test

extern int xp_mag_debug(char *type,char *fun,char *param);  //ma debug



#endif
//end of the file
